#ifndef VMMEM_H
#define VMMEM_H

#include "vmsys.h"

#ifdef __cplusplus 
extern "C" {
#endif

typedef struct mem_pool_t {
  char 	*base;
  int	size;
  int	count;
} mem_pool_t; 

#define MAX_MEM_POOL	2

#define VRE_KERNAL_MEMPOOL_IDX			(0)
#define VRE_USER_MEMPOOL_IDX			(1)

/**
 * Initialize the memory pool.
 * VRE provide the function of dynamic memory management.
 * The application privide a block of sequential memory, generally a byte-array in constant size.
 * This function must be called before vm_malloc and vm_free.
 * This is a system function, programmer should not call this function 
 * without understanding the bottom implementation, otherwise, the result is unpredictable.
 */
void vm_init_mempool(void* pool, int poolsize);

/**
 * Initialize the memory pool of partition management.
 * Partition management means that system divide the memory pool into multiple partitions, 
 * program can specified to active one of them, or delete the program data in one of them. 
 * This feature can avoid the memory leak caused by code error.
 * The initializing of memory martition start from 0 partition, until MAX_MEM_POOL - 1.
 *
 * @param idx		Partition No. less than MAX_MEM_POOL - 1, start from 0.
 * @param pool		Pointer of memory pool.
 * @param poolsize	Size of memory pool.
 */
void vm_init_multi_mempool(VMINT idx, void* pool, VMINT poolsize);

/**
 * Release the system memory pool managed by VRE, corresponding to vm_init_mempool.
 */
void vm_shutdown_mempool(void);

/**
 * Compute the size of memory chunk with specified memory.
 */
VMINT chunk_size(VMBYTE *p);

/**
 * Activate the memory partition, only be used after vm_init_multi_mempool.
 *  
 * @param idx		Partition No. start from 0.
 */
VMINT vm_activate_mem_partition(VMINT idx);

/**
 * Reset memory partition, in which all the user memory will be released.
 *  
 * @param idx		Partition No. start from 0.
 */
void vm_reset_mem_partition(VMINT idx);

#ifdef __cplusplus
}
#endif

#endif

